<?
	include("cms.php");
	
	ini_set('output_buffering', 'Off');
	error_reporting(E_ALL);
	
	$_SESSION['attachment_msg'] = array();
	
	if($_FILES){
		
		foreach($_FILES as $fieldname => $arr){
			
			if($arr['error']){
				$_SESSION['attachment_msg'][] = "Error: " . $arr['name'];
				echo "error";
			}else{
				
				$filename = $arr['name'];
				$ext = substr($filename, strrpos($filename, '.') + 1);
				$path = uniqid('f') . "." . $ext;
				
				//Move from tmp
				copy($arr['tmp_name'], "../cms-data/files/$path");

				
				$a = new Attachment();
				$a->cmsContent_idcontent = $_POST['idcontent'];
				$a->name = $arr['name'];
				$a->mime = $arr['type'];
				$a->path = $path;
				$a->comment = "";
				$a->downloads = 0;
				$a->bytes = $arr['size'];
				$a->hide = 0;
				$a->insert();
				
				Log::add(ATTACH, $_POST['idcontent'], $a->name);
				
			}//end if error
			
		}//end foreach
		
	}//end if _FILES
	
	header('Location: attchs.php?idcontent=' . $_POST['idcontent']);
?>